package com.stud.framework.runner;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * 系统启动时,初始化必要缓存
 * sys_cache_init.init_flag == 0,则执行初始化
 *
 * @author Stud
 */

@Component
@Order(4)
@Slf4j
@RequiredArgsConstructor
public class AsposeRegisterRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        try {
            registerExcel_v_22_6();
        } catch (Exception e) {
            log.info("注册Aspose.Cells失败");
            e.printStackTrace();
        }
    }

    private void registerExcel_v_22_6() throws Exception {
        String licenseExpiry = "20991231";

        // License
        Class<?> licenseClass = Class.forName("com.aspose.cells.License");
        Field a = licenseClass.getDeclaredField("a");
        a.setAccessible(true);
        a.set(null, licenseExpiry);

        // k65
        Class<?> k65Class = Class.forName("com.aspose.cells.k65");
        Field k65A = k65Class.getDeclaredField("a");
        k65A.setAccessible(true);

        Constructor<?> constructor = k65Class.getDeclaredConstructors()[0];
        constructor.setAccessible(true);
        Object k65Instance = constructor.newInstance();
        k65A.set(null, k65Instance);

        Field k56C = k65Class.getDeclaredField("c");
        k56C.setAccessible(true);
        k56C.set(k65Instance, licenseExpiry);

        // e0n
        Class<?> e0nClass = Class.forName("com.aspose.cells.e0n");
        Field e0nA = e0nClass.getDeclaredField("a");
        e0nA.setAccessible(true);
        e0nA.set(null, false);

    }
}
